﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeavenBAL;
using FortuneHeavenCommon.DataMap;
using System.Text;
using Jzy.Core.Common.Basic;
using Jzy.Core.Utility;
using Jzy.Core.Dict;
using FortuneHeaven.Admin.Common;
using FortuneHeavenCommon.ComMethod;

namespace FortuneHeaven.Admin.Pages.Auth
{
    public partial class AdminUserDetail : PageBase
    {
        public override string PageID
        {
            get { return "F41"; }
        }
        private string pre = "";
        public string Province;
        public string City;
        protected void Page_Load(object sender, EventArgs e)
        {
            Province = Request["slProvince"];
            City = Request["slCity"];
            if (!IsPostBack)
            {

                if (ID >= 0)
                {
                    AdminUserBO bo = new AdminUserBO();

                    AdminUser g = bo.GetUser(ID);
                    if (g.IsAdmin == 1)
                        return;
                    this.txtName.Text = g.LoginName;
                    this.txtPassword.Attributes.Add("value", g.Password);
                    
                    this.txtPassword.Text =  g.Password;
                    this.txtRealName.Text = g.RealName;
                    this.txtRoom.Text = g.RoomFilter;
                    this.drpFromPage.SelectedValue = g.IndexFilter;
                    Province = g.ProvinceFilter;
                    City = g.CityFilter;
                    InitalRoles(g);
                }
                else
                {
                    InitalRoles(null);
                }
            }
        }

        private void InitalRoles(AdminUser g)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("<select style=\" width:100px;height:150px\" id=\"slRoles\" size=\"5\">");
            if (g != null)
            {

                if (g.RoleList != null)
                {
                    foreach (Role r in g.RoleList)
                    {
                        builder.AppendLine(string.Format("<option value='{0}'>{1}</option>", r.ID, r.Name));
                    }
                }
                hidSelectRoles.Value = g.Roles;
            }
            builder.AppendLine("</select>");
            tdSelect.InnerHtml = builder.ToString();
            GenerateNoSelectInnerHtml(g);
        }

        private void GenerateNoSelectInnerHtml(AdminUser g)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("  <select runat=\"server\" style=\" width:100px;height:150px\"  id=\"slNoRoles\" size=\"5\">");
            RoleBO roles = new RoleBO();
            List<Role> content = roles.GetList();
            foreach (Role k in content)
            {
                if (g == null || !FindExist(k, g.RoleList))
                {
                    builder.AppendLine(string.Format("<option value='{0}'>{1}</option>", k.ID, k.Name));
                }
            }
            builder.AppendLine("</select>");
            tdNoSelect.InnerHtml = builder.ToString();
        }

        private bool FindExist(Role current, List<Role> content)
        {
            if (content != null)
            {
                foreach (Role index in content)
                {
                    if (index.ID == current.ID)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public new int ID
        {
            get
            {
                return string.IsNullOrEmpty(Request["ID"]) ? -1 : int.Parse(Request["ID"]);
            }
        }

        public string Pre
        {
            get
            {
                if (!string.IsNullOrEmpty(pre))
                    return pre;
                else
                    pre = Server.UrlDecode(Request["pre"]);
                return pre;
            }
        }

        public string CheckStatus()
        {
            if (ID < 0)
            {
                return "新增用户";
            }
            else
            {
                return "修改用户";
            }
        }


        public void Save(object sender, EventArgs arg)
        {
            if (!this.IsValid)
            {
                return;
            }

            AdminUserBO bo = new AdminUserBO();
            AdminUser g;
            if (ID >= 0)
            {
                g = bo.GetInfo(ID);

            }
            else
            {
                g = new AdminUser();
            }
            g.LoginName = this.txtName.Text;
            g.RealName = this.txtRealName.Text;
          
            g.Roles = hidSelectRoles.Value;
            g.RoomFilter = this.txtRoom.Text;
            g.ProvinceFilter = Province;
            g.CityFilter = City;
            g.IndexFilter = this.drpFromPage.SelectedValue;
            if (ID >= 0)
            {
                if (this.txtPassword.Text != g.Password)
                {
                    g.Password = CommonEncrypt.Encrypt(this.txtPassword.Text);
                }
                bo.Save(g, false);
                Response.Write(string.Format("<Html> <script>alert('修改成功');document.location ='{0}'; </script> </HTML>", Pre));

            }
            else
            {
                g.Password = CommonEncrypt.Encrypt(this.txtPassword.Text);
                g.ID = bo.GetMaxID();
                g.Time = DateTime.Now;
                bo.Save(g, true);
                Response.Write(string.Format("<Html> <script>alert('新增成功');document.location ='{0}'; </script> </HTML>", Pre));
                Response.Flush();
            }
        }
        public void CancelSave(object sender, EventArgs arg)
        {
            Response.Redirect(Pre);
        }
    }
}